package com.jash.himalaya;

import android.databinding.DataBindingUtil;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.jash.himalaya.databinding.ActivityMainBinding;
import com.jash.himalaya.entities.DaoMaster;
import com.jash.himalaya.entities.DaoSession;
import com.jash.himalaya.entities.DiscoveryColumnEntity;
import com.jash.himalaya.entities.FocusImageEntity;
import com.jash.himalaya.entities.FocusImageEntityDao;
import com.jash.himalaya.entities.FocusImages;
import com.jash.himalaya.entities.HomeEntity;
import com.jash.himalaya.entities.HotRecommendEntity;
import com.jash.himalaya.entities.RecommendEntity;
import com.jash.himalaya.utils.CommentRecyclerAdapter;
import com.jash.himalaya.utils.NetService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity implements Callback<HomeEntity> {
    private static final String TAG = MainActivity.class.getSimpleName();
    private ActivityMainBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private NetService service;
    private DaoSession session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        HashMap<Class<?>, Pair<Integer,Integer>> map = new HashMap<>();
        map.put(FocusImages.class, Pair.create(R.layout.focus_images, BR.images));
        map.put(DiscoveryColumnEntity.class, Pair.create(R.layout.discovery_item, BR.column));
        map.put(RecommendEntity.class, Pair.create(R.layout.recommend_item, BR.recommend));
        adapter = new CommentRecyclerAdapter<>(this, new ArrayList<>(), map);
        binding.mainList.setAdapter(adapter);
        service = new Retrofit.Builder()
                .baseUrl("http://www.baidu.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(NetService.class);
        service.getHome().enqueue(this);
        service.getHot().enqueue(this);
        session = DaoMaster.newDevSession(this, "himalaya-db");
        List<FocusImageEntity> entities = session.getFocusImageEntityDao().queryBuilder()
                .where(FocusImageEntityDao.Properties.Id.eq(13056))
                .list();
//        session.getFocusImageEntityDao().load()
        for (FocusImageEntity entity : entities) {
            Log.d(TAG, "onCreate: " + entity.getLongTitle());
        }
    }

    @Override
    public void onResponse(Call<HomeEntity> call, Response<HomeEntity> response) {
        HomeEntity body = response.body();
        FocusImages images = body.getFocusImages();
        if (images != null) {
            session.getFocusImageEntityDao().insertOrReplaceInTx(images.getList());
        }
        adapter.add(0, images);
        adapter.add(1, body.getDiscoveryColumns());
        adapter.add(2, body.getEditorRecommendAlbums());
        HotRecommendEntity hotRecommends = body.getHotRecommends();
        if (hotRecommends != null) {
            adapter.addAll(hotRecommends.getList());
        }
    }

    @Override
    public void onFailure(Call<HomeEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
